<?php
/**
 * Created by PhpStorm.
 * User: Charles
 * Date: 2017/10/9
 * Time: 19:58
 */

namespace app\Http\Controllers\ShortUrl;


use App\Http\Controllers\Controller;
use DB;

class RedirectController extends Controller
{
    public function index($code)
    {
        if (!is_string($code) && !strlen($code)) {
            abort(404);
        }
        $line = DB::table('short_urls')->where('key', $code)->first();
        if (null === $line) {
            abort(404);
        }
        $redirect_url = $line->url;
        DB::table('short_urls')->where('key', $code)->update(['count' => $line->count + 1]);
        return redirect($redirect_url);
    }
}